package com.txunda.zhuohired.aty.user.my;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.ants.theantsgo.AppManager;
import com.ants.theantsgo.config.Config;
import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.tools.AlertDialog;
import com.ants.theantsgo.util.L;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.aty.login.LoginAty;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.user.my.SettingBean;
import com.txunda.zhuohired.dialog.ServicePhoneDialog;
import com.txunda.zhuohired.http.pst.InfoPst;
import com.txunda.zhuohired.util.DateCleanUtil;

import java.util.Map;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/9 15:52
 * 功能描述：设置
 * 联系方式：jiahuanchen@126.com
 */

public class SettingAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * 缓存
     */
    @ViewInject(R.id.tv_setting_cache)
    private TextView tv_setting_cache;
    /**
     * 客服电话
     */
    @ViewInject(R.id.tv_setting_phone)
    private TextView tv_setting_phone;

    private ServicePhoneDialog servicePhoneDialog;
    private InfoPst infoPst;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("设置");
    }

    @Override
    @OnClick({R.id.tv_setting_modify_pass, R.id.ll_setting_cache, R.id.ll_setting_phone, R.id.tv_setting_opinion, R
            .id.tv_setting_about_as, R.id.tv_setting_login_out})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_setting_modify_pass://修改登录密码
                startActivity(ModifyPassAty.class, null);
                break;
            case R.id.ll_setting_cache://清除缓存
                showDialog("确认清除缓存?", 2);
                break;
            case R.id.ll_setting_phone://客服热线
                servicePhoneDialog = new ServicePhoneDialog(this, tv_setting_phone.getText().toString().trim(), new
                        ServicePhoneDialog
                                .CallPhoneListener() {
                            @Override
                            public void onCallPhone() {
                                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tv_setting_phone
                                        .getText()
                                        .toString().trim()));
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                            }
                        });
                servicePhoneDialog.show();
                break;
            case R.id.tv_setting_opinion://意见反馈
                startActivity(OpinionAty.class, null);
                break;
            case R.id.tv_setting_about_as://关于我们
                startActivity(AboutUsAty.class, null);
                break;
            case R.id.tv_setting_login_out://退出登录
                showDialog("确认退出当前账户?", 1);
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_setting;
    }

    public void showDialog(String tittle, final int type) {
        new AlertDialog(this).builder().setTitle("提示").setMsg(tittle)
                .setPositiveButton("确认", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        if (type == 1) {
                            loginOut();
                        } else if (type == 2) {
                            try {
                                DateCleanUtil.clearAllCache(SettingAty.this);
                                tv_setting_cache.setText(0 + "k");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        }).show();
    }

    private void loginOut() {
        Config.setLoginState(false);
        Map<String, String> map = application.getUserInfo();
        map.clear();
        application.setUserInfo(map);
        AppManager.getInstance().killAllActivity();
        startActivity(LoginAty.class, null);
    }

    @Override
    protected void initialized() {
        infoPst = new InfoPst(this);
        //显示缓存大小
        try {
            DateCleanUtil.getTotalCacheSize(SettingAty.this, tv_setting_cache);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void requestData() {
        infoPst.setConf();
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if(requestUrl.contains("setConf")){
//            L.e("=====setConf====",jsonStr);
            SettingBean settingBean = GsonUtil.GsonToBean(jsonStr,SettingBean.class);

            tv_setting_phone.setText(settingBean.getData().getTel());
        }
    }
}
